/**
 * <p>文件名:		DataBaseUtils.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		千方集团CTFO</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

/**
 * <p>
 * DataBaseUtils
 * </p>
 * <p>
 * 用途：数据库工具类
 * </p>
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-7-22
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-22 下午4:59:38</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-22 下午4:59:38</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class DataBaseUtils {

	/**
	 * 统一调用关闭连接
	 * @param rs 记录集
	 * @param ps 预编译语句
	 * @param con 连接
	 */
	public static void closeConnection(final ResultSet rs, final Statement ps, final Connection con, final Logger log) {
		closeResultSet(rs,log);
		closeStatement(ps, log);
		closeConnection(con, log);
	}
	/**
	 * 关闭连接对象 
	 * @param con
	 * @param log void
	 */
	private static void closeConnection(final Connection con, final Logger log) {
		try {
			if (con != null){
				con.close();
			}
		} catch (SQLException e) {
			if (log != null && log.isInfoEnabled()) {
				log.error("关闭conn对象异常msg=" + e.getLocalizedMessage());
			}
		}
	}
	/**
	 * 关闭会话对象 
	 * @param ps
	 * @param log void
	 */
	private static void closeStatement(final Statement ps, final Logger log) {
		try {
			if (ps != null){
				ps.close();
			}
		} catch (SQLException e) {
			if (log != null && log.isInfoEnabled()) {
				log.error("关闭ps对象异常msg=" + e.getLocalizedMessage());
			}
		}
	}
	/**
	 * 关闭记录集 
	 * @param rs	记录集对象
	 * @param log  日志对象，为空时不输出异常信息
	 */
	private static void closeResultSet(final ResultSet rs, final Logger log) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			if (log != null && log.isInfoEnabled()) {
				log.error("关闭rs对象异常msg=" + e.getLocalizedMessage());
			}
		}
	}
}
